#import "Buffer.h"
#import "Entity.h"
#import "Shop.h"
#import	"Item.h"

@implementation Buffer
@synthesize  map_type,gold,startX,startY,currentLayer, currentTile,firstPlayer,currentStage,stageName;
@synthesize  normaltxt, italictxt, smalltxt;
@synthesize  obs, zone, flash0,flash1,attack0,attack1;
@synthesize  shops,party,items,movieFrames,settings;

-(id)init{
[super init];
	self.smalltxt =  [UIFont systemFontOfSize:8];  
    self.normaltxt = [UIFont systemFontOfSize:10];  
    self.italictxt = [UIFont systemFontOfSize:12];  
	
    // add trivial images
    self.obs=[UIImage imageNamed:@"interface/tile_obstructed.gif"];
    self.flash0=[UIImage imageNamed:@"interface/flashtile_1.png"];
	self.flash1=[UIImage imageNamed:@"interface/flashtile_2.png"];
	self.attack0=[UIImage imageNamed:@"interface/atkrangetile_1.png"];
	self.attack1=[UIImage imageNamed:@"interface/atkrangetile_2.png"];
	
    return self; 
}

-(void) readSettings{
	NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
	self.settings= [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSMutableArray* frames = [self.settings objectForKey:@"movieFrames"];
	self.movieFrames = [NSMutableArray arrayWithCapacity:[frames count]];
    for(int i=0;i<[frames count];i++){
 		[self.movieFrames addObject:[UIImage imageNamed:[frames objectAtIndex:i]]];
    }
}

-(void) loadScene:(int)i{
	self.currentStage = i;
	[self initScriptFile];
//	[self initPartyFile];

}



-(void) initScriptFile
{
	NSMutableArray * scenes= [self.settings objectForKey:@"gameScenes"];
	NSMutableDictionary* scene =[scenes objectAtIndex:currentStage];
	self.startX =			[[scene objectForKey:@"startx"] intValue];
	self.startY =			[[scene objectForKey:@"starty"] intValue];
	self.stageName =		[scene objectForKey:@"nextmap"];
}
-(void) initPartyFile
{
    self.gold= [[self.settings objectForKey:@"goldstart"]intValue];
    NSMutableArray* characters = [self.settings objectForKey:@"characters"];
    for(int i=0;i<[characters count];i++){
		NSMutableDictionary* ch = [characters objectAtIndex:i];
		
        NSString* join_later = [ch objectForKey:@".joins_later"];
        if(join_later==@"no")
        {
            Entity* e =  [[Entity alloc] init ];
            //e.source    = [entfile objectForKey:@"source"];
            e.name  = [ch objectForKey:@"name"];
            e.classname = [ch objectForKey:@"class"];
            e.portrait=[UIImage imageNamed:[ch objectForKey:@"portrait"]];
            e.strength = [[ch objectForKey:@"strength"] intValue] ;
            e.precision = [[ch objectForKey:@"precision"] intValue];
            e.reflexes = [[ch objectForKey:@"reflexes"] intValue];
            e.endurance =[[ ch objectForKey:@"endurance"] intValue];
            e.speed = [[ch objectForKey:@"speed"] intValue];
            e.willpower =[[ ch objectForKey:@"willpower"] intValue];
			e.type         = [ch objectForKey:@"type"];
            
            int ai = [[ch objectForKey:@"ai"] intValue];
            if(ai>0) { e.ai = true; }
            else { e.ai = false;    }
			int level      = [[ch objectForKey:@"level"] intValue];
			e = [self genCharStats:e withLevel:level ];
			//new stuff
			e.item1 = [ch objectForKey:@".item1"];
			e.item2 = [ch objectForKey:@".item2"];
			e.item3 = [ch objectForKey:@".item3"];
			e.item4 = [ch objectForKey:@".item4"];
			e.weapon  = [ch objectForKey:@"weapon"];
			[self.party addObject:e];
		}
	}
}


-(void)resetToPartyOnly
{
}	
-(void)clearAllEnts
{
}
-(void)addEnt:(Entity*)e
{
}



-(void) placeParty{
	for(int i=0;i<[self.party count];i++) {
		Entity* e = [self.party objectAtIndex:i];
		e.x = 24;//*(self.startX-i/2);
		e.y = 24;//b*(self.startY-i%2);
	}
}

-(NSMutableDictionary*) readProperties:(NSString*)file{
	
	NSString *plistPath = [[NSBundle mainBundle] pathForResource:file ofType:@"plist"];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
	[plistDict retain];
	return plistDict;
}

-(NSMutableArray*)loadArray:(NSString*)name fromFile:(NSString*)file {
	NSString *path  = [[NSBundle mainBundle] pathForResource:file ofType:@"smf"];
			NSLog(@"data file found. reading into memory");
			NSMutableData *theData;
			NSKeyedUnarchiver *decoder;
			NSMutableArray *tempArray;
	
	theData = [NSData dataWithContentsOfFile:path];
	decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:theData];
	tempArray = [decoder decodeObjectForKey:name];
	[decoder finishDecoding];
	[decoder release];
	[tempArray retain];
return tempArray	;
}
//	
//	NSMutableArray* personArray;
//	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//	NSString *documentsDirectory = [paths objectAtIndex:0];
//	NSString *path = [documentsDirectory stringByAppendingPathComponent:file];
//	NSFileManager *fileManager = [NSFileManager defaultManager];
//	if([fileManager fileExistsAtPath:path]) {
//		NSLog(@"data file found. reading into memory");
//		NSMutableData *theData;
//		NSKeyedUnarchiver *decoder;
//		NSMutableArray *tempArray;
//		
//		theData = [NSData dataWithContentsOfFile:path];
//		decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:theData];
//		tempArray = [decoder decodeObjectForKey:name];
//		personArray =tempArray;
//		[decoder finishDecoding];
//		[decoder release];		
//	} else {
//		NSLog(@"no file found. creating empty array");
//		personArray = [[NSMutableArray alloc] init];
//	}
//	return personArray;
//}	
- (void) saveArray:(NSMutableArray *)array as:(NSString*)name toFile:(NSString*)file {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *path = [documentsDirectory stringByAppendingPathComponent:file];
	NSFileManager *fileManager = [NSFileManager defaultManager];
	if([fileManager fileExistsAtPath:path]) {
		NSLog(@"data file found. reading into memory");
		NSMutableData *theData;
		NSKeyedArchiver *encoder;
		
		theData = [NSMutableData data];
		encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData];
		[encoder encodeObject:array forKey:name];
		[encoder finishEncoding];
		
		[theData writeToFile:path atomically:YES];
		[encoder release];
	}
}


-(Entity*) genCharStats:(Entity*)e withLevel:(int) level{
	return e;
}

@end